#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>
#include <QTimer>
#include <QCloseEvent>
#include <QTime>
#include <QMenuBar>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QSpinBox>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
#include <QGroupBox>
#include <QGridLayout>
#include <QScrollArea>
#include <QTableWidget>
#include <QTextEdit>
#include <QDockWidget>
#include <QFileSystemWatcher>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void closeEvent(QCloseEvent *event) override;

private slots:
    void saveSettings();
    void loadSettings();
    void restoreDefaultSettings();
    void startService();
    void stopService();
    void restartService();
    void checkStatus();
    void showAbout();
    void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
    void refreshStatus();
    void updateSystemInfo();
    void showSettingsDialog();
    void installService();
    void uninstallService();

private:
    void createActions();
    void createMenus();
    void createCentralWidget();
    void createStatusBar();
    void createTrayIcon();
    void updateServiceStatus();
    void loadDefaultSettings();

    // Actions
    QAction *startAct;
    QAction *stopAct;
    QAction *restartAct;
    QAction *statusAct;
    QAction *quitAct;
    QAction *aboutAct;
    QAction *aboutQtAct;
    QAction *settingsAct;
    QAction *showMainAct;
    QAction *installAct;
    QAction *uninstallAct;
    QAction *viewLogsAct; // 添加查看日志动作

    // Menus
    QMenu *fileMenu;
    QMenu *serviceMenu;
    QMenu *helpMenu;

    // Form widgets
    QSpinBox *checkIntervalSpinBox;
    QSpinBox *highFreqThresholdSpinBox;
    QSpinBox *lowFreqThresholdSpinBox;
    QSpinBox *sustainedPeriodHighSpinBox;
    QSpinBox *sustainedPeriodLowSpinBox;
    QSpinBox *freqReductionSpinBox;
    QSpinBox *freqIncreaseSpinBox;
    QSpinBox *targetFreqAfterReductionSpinBox;
    QSpinBox *ioCriticalThresholdSpinBox;
    QSpinBox *ioSevereThresholdSpinBox;
    QSpinBox *ioHighThresholdSpinBox;

    QPushButton *saveButton;
    QPushButton *restoreDefaultButton;
    QPushButton *startServiceButton;
    QPushButton *stopServiceButton;
    QPushButton *restartServiceButton;
    QPushButton *installServiceButton;
    QPushButton *uninstallServiceButton;

    // StatusBar widgets
    QLabel *statusLabel;
    QLabel *serviceStatusLabel;
    QLabel *serviceStatusIcon;

    // Tray icon
    QSystemTrayIcon *trayIcon;
    QMenu *trayIconMenu;

    // Timer for refreshing status
    QTimer *refreshTimer;
    QTimer *systemInfoTimer;
    
    // System info table
    QTableWidget *systemInfoTable;
    
    // For network speed calculation
    quint64 lastReceivedBytes;
    quint64 lastTransmittedBytes;
    QDateTime lastNetworkTime;
    
    // For disk IO speed calculation
    quint64 lastReadSectors;
    quint64 lastWriteSectors;
    QDateTime lastIOTime;
    
    // Log viewer
    QDockWidget *logDockWidget; // 添加日志停靠窗口
    QTextEdit *logTextEdit;     // 添加日志文本显示区域
    QString logFilePath;        // 日志文件路径
    QFileSystemWatcher *logWatcher; // 文件系统监视器，用于监听日志文件变化
};

#endif // MAINWINDOW_H